//
//  NewFeatureController.swift
//  SinaWeibo
//
//  Created by 郭平 on 15/10/12.
//  Copyright © 2015年 郭平. All rights reserved.
//

import UIKit

class NewFeatureController: UIViewController, UIScrollViewDelegate{

    var pageControl: UIPageControl?
    
    override func viewDidLoad() {
        super.viewDidLoad()

        let scrollView = UIScrollView(frame: UIScreen.mainScreen().bounds)
        
        scrollView.delegate = self
        
        scrollView.backgroundColor = UIColor.whiteColor()
        scrollView.pagingEnabled =  true
        scrollView.showsVerticalScrollIndicator = false
        
        let count = 4
        
        for i in 0..<count {
            
            let imageView = UIImageView()
            
            imageView.image = UIImage(named: "new_feature_\(i + 1)")
            imageView.size = scrollView.size
            imageView.x = CGFloat(i) * scrollView.width
            
            if i == count - 1 {
                
               setupLastPage(imageView)
            }
            
            scrollView.addSubview(imageView)
        }
        
        scrollView.contentSize = CGSizeMake(CGFloat(count) * scrollView.width, 0)
        
        view.addSubview(scrollView)
        
        
        let control = UIPageControl()
        
        control.numberOfPages = count
        control.centerX = scrollView.width * 0.5
        control.y = SCREENH - 100
        
        control.pageIndicatorTintColor = UIColor.blueColor()
        control.currentPageIndicatorTintColor = UIColor.orangeColor()
        
        view.addSubview(control)
        
        self.pageControl = control
    }
    
    // 设置最后一张imageView的点击事件
    private func setupLastPage(imageView: UIImageView){
    
        imageView.userInteractionEnabled = true
        
        // 进入按钮
        let enterButton = UIButton()
        
        enterButton.addTarget(self, action: "enterButtonClick", forControlEvents: UIControlEvents.TouchUpInside)
        
        enterButton.setBackgroundImage(UIImage(named: "new_feature_finish_button_highlighted"), forState: UIControlState.Highlighted)
        enterButton.setBackgroundImage(UIImage(named: "new_feature_finish_button"), forState: UIControlState.Normal)
        
        enterButton.setTitle("进入微博", forState: UIControlState.Normal)
        enterButton.sizeToFit()
        enterButton.centerX = SCREENW * 0.5
        enterButton.y = SCREENH - 200
        
        imageView.addSubview(enterButton)
        
        // 分享按钮
        let sharedButton = UIButton()
        
        sharedButton.addTarget(self, action: "shareButtonClick:", forControlEvents: UIControlEvents.TouchUpInside)
        
        sharedButton.setTitleColor(UIColor.darkGrayColor(), forState: UIControlState.Normal)
        
        sharedButton.setImage(UIImage(named: "new_feature_share_false"), forState: UIControlState.Normal)
        sharedButton.setImage(UIImage(named: "new_feature_share_true"), forState: UIControlState.Selected)
        
        sharedButton.setTitle(" 分享到微博", forState: UIControlState.Normal)
        sharedButton.sizeToFit()
        sharedButton.centerX  = enterButton.centerX
        sharedButton.y = enterButton.y - 50
        
        imageView.addSubview(sharedButton)
        
    }
    
    @objc private func enterButtonClick() {
        
        let delegate = UIApplication.sharedApplication().delegate as! AppDelegate
        let window = delegate.window!
        
        window.rootViewController = delegate.defaultViewController()
        
    }
    
    
    @objc private func shareButtonClick(button: UIButton){
    
        button.selected = !button.selected
        
    }
    
    // MARK: - scrollViewDelegate
    
    func scrollViewDidScroll(scrollView: UIScrollView) {
        
        let page = round(scrollView.contentOffset.x / scrollView.width)
        
        self.pageControl?.currentPage = Int(page)
        
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

}
